package com.smsc.headend.task.engine.utils;

import com.smsc.headend.module.mdm.ws.WsTokenResultEnum;

/**
 * @author wxh
 * @date 2021/6/15 19:28
 */
public class WsTokenMapUtil {
    /**
     * 威胜提供的Vending错误码映射接口如下
     * status_code 为05的有重复项 返回 1即可, 其他所有情况映射不上的统一返回12 val_TokenFailure解析的bitString获取不到 暂时忽略不影响结果
     *
     * 03 01 05 01 0B 0100 08 00 04  00
     * L1 AR L2 VR L3 TR   L4 ID    UF1
     *
     *IDRecordResult 及 TokenFailure位数不确定 暂时不进行判断 只需判断前面 status_code  val_AuthenticationResult	val_ValidationResult	val_TokenResult
     *
     * 	status_code  val_AuthenticationResult	val_ValidationResult	val_TokenResult	 val_IDRecordResult	val_TokenFailure  Vending_system_result_code
     *  0x03	     0x01/0x00	                0x01/0x00	            0x0100/0x0000	 0x00	            0x00	           0
     *  0x05	     0x02	                    0x00	                0x0000	         0x00	            0x00	           1
     *  0x06	     0x00	                    0x04	                0x0000	         0x00	            0x00	           2
     *  0x06	     0x00	                    0x02	                0x0000	         0x00	            0x00	           3
     *  0x06	     0x00	                    0x08	                0x0000	         0x00	            0x00	           4
     *  0x07	     0x00	                    0x00	                0x0020	         0x00	            0x00	           5
     *  0x06	     0x00	                    0x10	                0x0000	         0x00	            0x00	           6
     *  0x05	     0x04	                    0x00	                0x0000	         0x00	            0x00	           7
     *  0x07	     0x00	                    0x00	                0x0000	         0x00	            0x02	           8
     *  0x07	     0x00	                    0x00	                0x0040	         0x00	            0x00	           9
     *  0x07	     0x00	                    0x00	                0x0100	         0x00	            0x00	           10
     *  0x05	     0x02	                    0x00	                0x0000	         0x00	            0x00	           11
     *  0x07	     0x00	                    0x00	                0x0000	         0x00	            0x00	           12
     */

    public static int getResultCode(Integer statusCode, String combineStr) {
        int result;
        String tokenesult = parseTokenResult(statusCode,combineStr);
        switch (tokenesult) {
            case "0301010100":
            case "0301010000":
            case "0301000100":
            case "0301000000":
            case "0300010100":
            case "0300010000":
            case "0300000100":
            case "0300000000":
                result = WsTokenResultEnum.TOKEN_PROC_SUCCESS.code();
                break;
            case "0502000000":
                result = WsTokenResultEnum.TOKEN_INTERPRETATION_ERROR.code();
                break;
            case "0600040000":
                result = WsTokenResultEnum.TOKEN_USED.code();
                break;
            case "0600020000":
                result = WsTokenResultEnum.TOKEN_EXPIRED.code();
                break;
            case "0600080000":
                result = WsTokenResultEnum.KEY_EXPIRED.code();
                break;
            case "0700002000":
                result = WsTokenResultEnum.RECHARGE_EXCEED_LIMIT.code();
                break;
            case "0600100000":
                result = WsTokenResultEnum.KEY_TYPE_NOT_ALLOW_RECHARGE.code();
                break;
            case "0504000000":
                result = WsTokenResultEnum.TEST_TOKEN_GEN_NON_MFG.code();
                break;
            case "0700000000":
                result = WsTokenResultEnum.TOKEN_NOT_CONSEQUENT.code();
                break;
            case "0700004000":
                result = WsTokenResultEnum.KEY_TYPE_CHANGE_ERROR.code();
                break;
            case "0700000001":
                result = WsTokenResultEnum.KEY_TYPE_INCORRECT.code();
                break;
            default:
                result = WsTokenResultEnum.TOKEN_SET_PARAM_INVALID.code();
        }
        return result;
    }

    private static String parseTokenResult(Integer statusCode, String combineStr) {

        StringBuilder sb = new StringBuilder();
        sb.append(String.format("%02X", statusCode)).append(combineStr, 4, 6).append(combineStr, 8, 10).append(combineStr, 12, 16);
        return sb.toString();
    }
}
